/*
 * 专门为面板添加事件控制
 * @memberOf {TypeName} 
 */
Ext.define("AM.controller.deptController", {
	extend: 'Ext.app.Controller',
	init: function() {
		this.control({
			"deptTree": {
				checkchange: function(node, checked, options) {
					alert(111);
					if (node.data.leaf == false) {  //非叶子节点
						if (checked) {
							node.expand();// 打开节点
							//遍历孩子节点
							node.eachChild(function(n) {
								n.data.checked = true;
								n.updateInfo({checked: true});
							});
						} else {
							node.expand();// 打开节点
							//遍历孩子节点
							node.eachChild(function(n) {
								n.data.checked = false;
								n.updateInfo({checked: false});
							});
						}
					} else {
						if (!checked) {
							node.parentNode.data.checked = false;
							node.parentNode.updateInfo({checked: false});
						}
					}
				}  // end function
			},
			//xpath方式获取某个面板上的标签
			"deptTree button[id='allOpen']":　{
				click: function(b, e) {  //添加单击事件
					var tree = b.ownerCt.ownerCt;
					tree.expandAll();  //展开所有
				}
			},
			"deptTree button[id='allClose']":　{
				click: function(b, e) {
					var tree = b.ownerCt.ownerCt;
					tree.collapseAll();
				}
			},
			"deptTree button[id='add']":　{
				click: function(b, e) {
					//获取面板对象
					var tree = b.ownerCt.ownerCt;
					//找到被选中的节点
					var nodes = tree.getChecked();
					if (nodes.lenght == 1) {
						var node = nodes[0];
						node.appendChild({  //添加一个节点
							id: '010001',
							checked: false,
							text: '技术add部',
							leaf: true
						});
					} else {
						Ext.MessageBox.alert("请选择一个节点！");
					}
				}
			},
			"deptTree button[id='delete']":　{
				click: function(b, e) {
					//获取面板对象
					var tree = b.ownerCt.ownerCt;
					//找到被选中的节点
					var nodes = tree.getChecked();
//					if (nodes.lenght > 0) {
						for (var i = 0; i < nodes.length; i++) {
							nodes[i].remove(true);
						}
//					} else {
//						Ext.MessageBox.alert("请选择一个节点！");
//					}
				}
			},
			"deptTree button[id='copy']":　{  //复制监听
				click: function(b, e) {
					var tree = b.ownerCt.ownerCt;
					var nodes = tree.getChecked();
					if (nodes.length > 0) {
						//把数据放到剪切板中
						tree.setCopyNodes(Ext.clone(nodes));
						var str = "";
						for (var i = 0; i < nodes.length; i++) {
							str += nodes[i].data['text'] + " ";
						}
						Ext.MessageBox.alert("提示信息", "您复制了 " + str);
						for (var i = 0; i < nodes.length; i++) {
							nodes[i].data.checked = false;
							nodes[i].updateInfo();
						}
					}
				}
			},
			"deptTree button[id='paste']":　{  //粘贴
				click: function(b, e) {
					var tree = b.ownerCt.ownerCt;
					//被追加孩子的节点
					var checkedNodes = tree.getChecked();
					//alert(checkedNodes.length);
					if (checkedNodes.length == 0) {
						Ext.MessageBox.alert("提示信息", "请选择一个您要粘贴的目标！！");
					}
					if (1 == checkedNodes.length) {
						var node = checkedNodes[0];
						//去剪切板中取出复制的数据
						var nodes = tree.getCopyNodes();
						if (nodes.length > 0) {
							for (var i = 0; i < nodes.length; i++) {
								var n = nodes[i].data;
								n['id'] = n['id'] + '1';
								node.appendChild(n);
							}
						}
					} else {
						Ext.MessageBox.alert("剪切板中没有选择要复制的节点！！");
					}
				}
			},
			"deptTree": {
				itemclick: function(tree, record, item, index, e, opts) {
					//alert(record.get('id'));
				}
			}
		});
	},
	views: ['deptView'],  //关联view层
	stores: ['deptStore'],  //关联store层
	models: ['deptModel']
});